#pragma once
#include <metalc/errhdl/_errhdl.h>
#include <dinfra/mplf/tstack.h>
/* 将内存放入隔离区 */
static tstack_t corrupted_mem_quarantine;
typedef struct{
	tsnode_t nd;
	size_t size;
}quaranode_t;

SAI bool_t merr_raise_quarantine(void *ptr, size_t size, merr_t errno){
	INFO("%p, sz %lu is corrupted, push to quarantine, errno %u\n", ptr, size, errno);
	ASSERT(size >= sizeof(quaranode_t));
	quaranode_t *node = (quaranode_t *)ptr;
	node->size = size;
	tstack_push(&corrupted_mem_quarantine, &node->nd);
	return TRUE;
}
/* 结束时打印 */
SAI void merr_quarantine_print(){
	quaranode_t *nd = tstack_entry(corrupted_mem_quarantine.head, quaranode_t, nd);
	size_t sz = 0;
	dbg_printf("----------quaratine stack start----------\n");
	while(nd){
		dbg_printf("[%p]%lu\n", (void *)nd, nd->size);
		sz+= nd->size;
	}
	dbg_printf("total size %lu\n", sz);
	dbg_printf("----------quaratine stack end----------\n");
}

#define PRINT_QUARANTINE	\
static __attribute__((destructor)) void __merr_quarantine_print(){\
	merr_quarantine_print();\
}
